angular.module('Controller')
.controller('QgyApartmentEditCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    type=items.type
    $scope.type=items.type
    $scope.data=items.data
    $scope.modalTitle=if type=='new' then '新建公寓' else '编辑公寓'
    $scope.modalEnableList=commonConstant.enabledList()
    _.defer(()->
        $('#photo-upload-input').fileinput({
            language: 'zh',
            maxFileCount: 1,
            allowedFileExtensions : ['jpg', 'png','gif'],
            uploadUrl: urls.backend + urls.imageUploadTo +  "/image?token=" + $localStorage['token'],
            uploadAsync: true,
            enctype: 'multipart/form-data',
            showCaption: false
        }).on('fileuploaded', (event, data, id, index)->
            if data.response?.code==0
                console.log(data.response?.message)
                saveUrl = data.response.message.saveUrl+""
                if(saveUrl.indexOf("http")==0)
                    $scope.data.thumb = saveUrl
                else
                    $scope.data.thumb=urls.backend+'/public/'+$scope.result['saveUrl']

                $scope.showPhotoUpload=false
                $scope.$digest()
            else
                wfAlert.toastError(data.response.message)
        )
    )
    notPdf=()->
        fName=$('#contractDemoInput').val()
        if  fName==undefined || fName=='' || fName.split('.').length<2
            return true
        else
            tmp=fName.substring(fName.lastIndexOf('.')+1)
            if tmp in ['pdf']
                return false
            else
                return true
    _.extend $scope,
        uploadContractDemo:()->
            $('#contractDemoInput').change(()->
                console.log($('#contractDemoInput').val())
                if notPdf()
                    wfAlert.toastInfo('合同模板只接受pdf文件')
                    return
                formData = new FormData()
                formData.append('file', $('#contractDemoInput')[0].files[0])
                $http({
                    url:urls.imageUploadTo +  "/image"
                    method:'POST'
                    headers:{
                        'Content-Type': undefined
                    }
                    data:formData
                    transformRequest: angular.identity
                }).success((resp)->
                    if resp.code==0
                        console.log('合同模板文件上传成功')
                        $scope.data.street = resp.message['path']
                )
            )
            $('#contractDemoInput').click()
            ''
        newData:()->
            reqData={
                "name":  $scope.data.name
                "uuid":  $scope.data.uuid
                "brief":$scope.data.brief
                "minPrice":$scope.data.minPrice
                "tel":$scope.data.tel
                "thumb":$scope.data.thumb
                "priceDesc":$scope.data.priceDesc
                "address":$scope.data.address
                "manageFee":$scope.data.manageFee
                "enabled":$scope.data.enabled
                "description":$scope.data.description
                #add
                "street":$scope.data.street
                "beforeFee":$scope.data.beforeFee
                "deliverDay":$scope.data.deliverDay
                "corporation":$scope.data.corporation
                "powerPrice":$scope.data.powerPrice
                "waterPrice":$scope.data.waterPrice
                "cleanPrice":$scope.data.cleanPrice
                "carRental":$scope.data.carRental
                "carFee":$scope.data.carFee
                "carStandard":$scope.data.carStandard
            }
            $http.post('/apartment',reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
        editData:()->
            reqData={
                "name":  $scope.data.name
                "uuid":  $scope.data.uuid
                "brief":$scope.data.brief
                "minPrice":$scope.data.minPrice
                "priceDesc":$scope.data.priceDesc
                "tel":$scope.data.tel
                "thumb":$scope.data.thumb
                "address":$scope.data.address
                "manageFee":$scope.data.manageFee
                "enabled":$scope.data.enabled
                "description":$scope.data.description
                #add
                "street":$scope.data.street
                "beforeFee":$scope.data.beforeFee
                "deliverDay":$scope.data.deliverDay
                "corporation":$scope.data.corporation
                "powerPrice":$scope.data.powerPrice
                "waterPrice":$scope.data.waterPrice
                "cleanPrice":$scope.data.cleanPrice
                "carRental":$scope.data.carRental
                "carFee":$scope.data.carFee
                "carStandard":$scope.data.carStandard
            }
            $http.put("/apartment/#{$scope.data.id}",reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)